package HelloProvider

import (
	"encoding/json"
	"fmt"
	"goGrpc/helloService/Service"
	pb "goGrpc/proto/protoGo" // 引入编译生成的包
	"golang.org/x/net/context"
	"log"
)

// 定义helloService并实现约定的接口
type helloService struct{}

// HelloService Hello服务
var HelloService = helloService{}

// SayHello 实现Hello服务接口
func (h helloService) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloResponse, error) {
	log.Printf("服务端接受数据: %v \n", in)
	log.Printf("ctx: %v \n", ctx)
	log.Printf("allParams: %v \n", in.AllParmas)

	// 将字节切片映射到指定map上
	// key：string类型，value：interface{}  类型能存任何数据类型
	var jsonInput map[string]interface{}
	err := json.Unmarshal([]byte(in.AllParmas), &jsonInput)
	if err != nil {
		log.Printf("json转义失败: %v \n", err)
	}
	// 打印对象结构
	log.Printf("jsonInput: %v \n", jsonInput)
	log.Printf("es: %v, \n", jsonInput["es"])

	resp := new(pb.HelloResponse)
	resp.Message = fmt.Sprintf("Hello %s.", in.Name)
	HelloServiceDo.MyHelloSql()
	HelloServiceDo.MyEsTest()
	HelloServiceDo.RedisTest()
	HelloServiceDo.MyHelloNeo4j()
	HelloServiceDo.KafaProduceHello()
	return resp, nil
}
